# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'dialog_add_index.ui'
##
## Created by: Qt User Interface Compiler version 6.3.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
import pymysql
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
                            QMetaObject, QObject, QPoint, QRect,
                            QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
                           QFont, QFontDatabase, QGradient, QIcon,
                           QImage, QKeySequence, QLinearGradient, QPainter,
                           QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QAbstractButton, QApplication, QDialog, QDialogButtonBox,
                               QHBoxLayout, QHeaderView, QLabel, QLineEdit,
                               QSizePolicy, QTableWidget, QTableWidgetItem, QToolButton,
                               QVBoxLayout, QWidget, QComboBox, QMessageBox, QSpacerItem, QLayout, QListWidgetItem)

from global_variables import GlobalVariables


class DialogAddIndex(QDialog):
    def __init__(self, parent, app):
        super(DialogAddIndex, self).__init__(parent)
        self.__list_field_data__ = []
        self.__app__ = app
        self.__mysqldb__ = GlobalVariables.DATABASE_OBJ
        self.set_list_field()
        self.setupUi(self)
        self.init()

    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(400, 300)
        self.verticalLayout = QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.verticalLayout.setSizeConstraint(QLayout.SetFixedSize)
        self.widget_2 = QWidget(Dialog)
        self.widget_2.setObjectName(u"widget_2")
        self.verticalLayout_3 = QVBoxLayout(self.widget_2)
        self.verticalLayout_3.setObjectName(u"verticalLayout_3")
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.label = QLabel(self.widget_2)
        self.label.setObjectName(u"label")

        self.horizontalLayout.addWidget(self.label)

        self.index_name = QLineEdit(self.widget_2)
        self.index_name.setObjectName(u"index_name")

        self.horizontalLayout.addWidget(self.index_name)


        self.verticalLayout_3.addLayout(self.horizontalLayout)

        self.horizontalLayout_3 = QHBoxLayout()
        self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
        self.label_3 = QLabel(self.widget_2)
        self.label_3.setObjectName(u"label_3")

        self.horizontalLayout_3.addWidget(self.label_3)

        self.widget_4 = QWidget(self.widget_2)
        self.widget_4.setObjectName(u"widget_4")
        self.horizontalLayout_4 = QHBoxLayout(self.widget_4)
        self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
        self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.index_type = QComboBox(self.widget_4)
        self.index_type.setObjectName(u"index_type")

        self.horizontalLayout_4.addWidget(self.index_type)

        self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        self.horizontalLayout_4.addItem(self.horizontalSpacer)


        self.horizontalLayout_3.addWidget(self.widget_4)


        self.verticalLayout_3.addLayout(self.horizontalLayout_3)

        self.widget = QWidget(self.widget_2)
        self.widget.setObjectName(u"widget")
        self.verticalLayout_2 = QVBoxLayout(self.widget)
        self.verticalLayout_2.setObjectName(u"verticalLayout_2")
        self.verticalLayout_2.setContentsMargins(0, -1, 0, -1)
        self.widget_3 = QWidget(self.widget)
        self.widget_3.setObjectName(u"widget_3")
        self.horizontalLayout_2 = QHBoxLayout(self.widget_3)
        self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
        self.horizontalLayout_2.setContentsMargins(0, 0, -1, -1)
        self.label_2 = QLabel(self.widget_3)
        self.label_2.setObjectName(u"label_2")

        self.horizontalLayout_2.addWidget(self.label_2)

        self.add_column = QToolButton(self.widget_3)
        self.add_column.setObjectName(u"add_column")

        self.horizontalLayout_2.addWidget(self.add_column)


        self.verticalLayout_2.addWidget(self.widget_3)

        self.column_table_list = QTableWidget(self.widget)
        self.column_table_list.setObjectName(u"column_table_list")

        self.verticalLayout_2.addWidget(self.column_table_list)


        self.verticalLayout_3.addWidget(self.widget)


        self.verticalLayout.addWidget(self.widget_2)

        self.buttonBox = QDialogButtonBox(Dialog)
        self.buttonBox.setObjectName(u"buttonBox")
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)

        self.verticalLayout.addWidget(self.buttonBox)


        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle("添加索引")
        self.label.setText(QCoreApplication.translate("Dialog", u"\u7d22\u5f15\u540d\u79f0\uff1a", None))
        self.label_3.setText(QCoreApplication.translate("Dialog", u"\u7d22\u5f15\u7c7b\u578b\uff1a", None))
        self.label_2.setText(QCoreApplication.translate("Dialog", u"\u7d22\u5f15\u5217\uff1a", None))
        self.add_column.setText(QCoreApplication.translate("Dialog", u"+", None))
    # retranslateUi


    def init(self):
        # 添加默认表格,列数共一列，表头名称
        self.column_table_list.setColumnCount(1)
        self.column_table_list.setHorizontalHeaderItem(0, QTableWidgetItem("列序列"))
        self.column_table_list.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.add_row()

        # 添加索引类型选项
        self.index_type.addItems([
            "INDEX",
            "UNIQUE",
            "PRIMARY",
            "FULLTEXT",
        ])

        # 设置添加列点击回调
        self.add_column.clicked.connect(self.add_column_clicked)
        self.buttonBox.accepted.disconnect(self.accept)
        self.buttonBox.accepted.connect(self.button_box_accepted)
        self.buttonBox.rejected.connect(self.reject)

    def button_box_accepted(self):
        self.accept()

        index_columns = []
        for row in range(0, self.column_table_list.rowCount()):
            box: QComboBox = self.column_table_list.cellWidget(row, 0)
            index_columns.append(box.currentText())
        if len(index_columns) == 0:
            return
        table_name_widget = self.__app__.field_list_fill_data.ui.table_list.currentItem()
        if not isinstance(table_name_widget, QListWidgetItem) or len(table_name_widget.text()) == 0:
            return
        table_name = table_name_widget.text()
        if len(self.index_name.text()) > 0:
            index_name = self.index_name.text()
        else:
            index_name = ""
        index_type = self.index_type.currentText()
        try:
            self.__mysqldb__.table_add_index(
                table_name=table_name,
                index_type=index_type,
                index_name=index_name,
                columns=index_columns,
            )
        except (pymysql.err.DataError, pymysql.err.OperationalError) as e:
            code, msg = e.args
            QMessageBox.critical(self, '添加索引错误', str(code) + "\t" + msg)
            raise e
        self.__app__.field_list_fill_data.refresh_index_clicked()

    def add_row(self):
        rowCount = self.column_table_list.rowCount()
        self.column_table_list.setRowCount(rowCount + 1)
        box = QComboBox(self.column_table_list)
        box.addItems(self.__list_field_data__)
        self.column_table_list.setCellWidget(rowCount, 0, box)

    def set_list_field(self):
        self.__list_field_data__ = [col.get('COLUMN_NAME') for col in self.__app__.field_list_fill_data.list_field_data]

    def add_column_clicked(self):
        if self.column_table_list.rowCount() >= len(self.__list_field_data__) - 1:
            return
        self.add_row()
